﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CrediSoftDemo.Models
{
    public class GestionCategoriaModel
    {
        public string prueba(GestionCategoriaDao jason)
        {
            string cad = "";
            using (northwindEntities bd = new northwindEntities())
            {
                var datos = (from p in bd.categories select p).ToList();
                if (datos.Count() > 0)
                    cad = datos[0].CategoryID.ToString();
                else cad = "nooooooooooooooooo";
            }
            return cad;
        }

        public category insert(GestionCategoriaDao jason)
        {
            category catego = new category();
            catego.CategoryID = jason.CategoryID;
            catego.CategoryName = jason.CategoryName;
            catego.Description = jason.Description;
            using (northwindEntities bd = new northwindEntities())
            {
                bd.categories.Add(catego);
                bd.SaveChanges();
            }
            return catego;
        }

        public List<category> search(GestionCategoriaDao jason)
        {
            List<category> result = new List<category>();
            using (northwindEntities bd = new northwindEntities())
            {
                var encontrado = (from p in bd.categories
                                  where p.CategoryName.Contains(jason.CategoryName)
                                  select p).ToList();
                if (encontrado.Count() > 0)
                {
                    foreach (category cat in encontrado)
                    {
                        result.Add(cat);
                    }
                }
            }
            return result;
        }

        public string modify(GestionCategoriaDao jason)
        {
            string cad = null;
            using (northwindEntities bd = new northwindEntities())
            {
                category cat = bd.categories.First(i => i.CategoryID == jason.CategoryID);
                if (cat != null)
                {
                    cat.CategoryName = jason.CategoryName;
                    cat.Description = jason.Description;
                }
                try
                {
                    bd.SaveChanges();
                    cad = +cat.CategoryID + " se modificó exitosamente.";
                }
                catch (Exception ex)
                {
                    cad = ex.ToString();
                }
            }
            return cad;
        }

        public string delete(GestionCategoriaDao jason)
        {
            string cad = "";
            using (northwindEntities bd = new northwindEntities())
            {
                category cat = bd.categories.First(i => i.CategoryID == jason.CategoryID);

                try
                {
                    bd.categories.Remove(cat);
                    bd.SaveChanges();
                    cad = "La categoria " + cat.CategoryName + " ha sido eliminada.";
                }
                catch (Exception ex)
                {
                    cad = ex.ToString();
                }
            }

            return cad;
        }
 
    }
}